/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.PermissionGroupEntity;
import team.mai.antexpress.entities.UserEntity;
import team.mai.antexpress.utils.Validator;

import java.util.List;

@Service
public class UserService {

	@Autowired
	private UserDAO userDAO;

	public static final int VALIDATION_ERROR = -2;
	public static final int INTERNAL_ERROR = -1;
	public static final int INSUFFICIENT_PRIVILEGE = -3;
	public static final int SUCCESS = 0;

	/**
	 * by Lief
	 */
	public int insertUser(String operatorUsername, UserEntity userToInsert) {
		boolean validate = Validator.validateUserCredentials(userToInsert.getUsername(), userToInsert.getPassword());
		if (!validate)
			return VALIDATION_ERROR;

		PermissionGroupEntity permission = userDAO.getPermissionByUser(operatorUsername);
		System.out.println("Operator Permission: " + permission);
		if (permission == null) {
			return INTERNAL_ERROR;
		}
		if (permission.isAdmin() || permission.isModifyUser()) {
			int insert = userDAO.insert(userToInsert);
			System.out.println("DAO returning " + insert);
			return insert;
		} else
			return INSUFFICIENT_PRIVILEGE;
	}

	/**
	 * by Jason
	 */
	public boolean changePassword(String username, String password, String newPassword){
        if(userDAO.verifyLogin(username,password)){
            return userDAO.changePassword(username,newPassword);
        }else {
            return false;
        }
		// returns true if success
	}

	// 以下Lief
	public UserEntity getUser(String opName, Integer id){
		if(id == null)
			return null;
		else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupUser()) {
				return userDAO.getUserById(id);
			} else {
				return null;
			}
		}
	}
	public UserEntity getUser(String opName, String username){
		if(username == null)
			return null;
		else {
			PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
			if (permissionByUser.isAdmin() || permissionByUser.isLookupUser()) {
				return userDAO.getUserByUsername(username);
			} else {
				return null;
			}
		}
	}


	public int deleteUser(String opName, int id){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyUser()) {
			//refuse if trying to delete himself
			if(opName.equals(userDAO.getUserById(id).getUsername()))
				return INSUFFICIENT_PRIVILEGE;
			if(userDAO.deleteUserById(id))
				return SUCCESS;
			return INTERNAL_ERROR;
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int deleteUsers(String opName, List<Integer> ids){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyUser()) {
			//If the operator himself is included
			if(ids.contains(userDAO.getUserByUsername(opName).getId()))
				return INSUFFICIENT_PRIVILEGE;
			if(userDAO.deleteUsersByIds(ids))
				return SUCCESS;
			else
				return INTERNAL_ERROR;
		} else{
			return INSUFFICIENT_PRIVILEGE;
		}
	}

	public int updateUser(String opName, UserEntity user){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
		if (permissionByUser.isAdmin() || permissionByUser.isModifyUser()) {
			if(userDAO.updateUser(user))
				return SUCCESS;
			return INTERNAL_ERROR;
		} else {
			return INSUFFICIENT_PRIVILEGE;
		}
	}
	public List<UserEntity> getAllUsers(String opName){
		PermissionGroupEntity permissionByUser = userDAO.getPermissionByUser(opName);
		if (permissionByUser.isAdmin() || permissionByUser.isLookupUser()) {
			return userDAO.getAllUsers();
		} else {
			return null;
		}
	}

}
